package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.result.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"permission:list","权限列表"},logical = Logical.OR)
    public String list(Model model,QueryObject qo){
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        PageInfo<Permission> pageResult = permissionService.query(qo);
        model.addAttribute("pageInfo", pageResult);
        return "permission/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"permission:delete","权限删除"},logical = Logical.OR)
    public String delete(Long id,QueryObject qo){
        permissionService.delete(id);
        int currentPage = qo.getCurrentPage();
        return "redirect:/permission/list.do?currentPage="+currentPage;
    }


    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        try {
            permissionService.reload();
            return new JsonResult( );
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult("加载失败", false);
        }
    }

   /* @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        if(id != null){
            Permission permission = permissionService.selectByPrimaryKey(id);
            model.addAttribute("permission", permission);
        }
        return "permission/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission,Long[] ids){
        if(permission.getId() != null){
            permissionService.update(permission);
        }else {
            permissionService.insert(permission,ids);
        }
        return "redirect:/permission/list.do";
    }*/
}
